package com.example.android427;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;


import com.example.android427.Fragment.gerenFragment;
import com.example.android427.Fragment.shouyeFragment;
import com.example.android427.Fragment.shujiaFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private ViewPager mviewpager;
    private FragmentPagerAdapter mAdpter; //适配器
    private List<Fragment> mFragments; //数据源

    private LinearLayout mshouye;//首页
    private LinearLayout mshujia;//书架
    private LinearLayout mgeren;//个人

    private ImageButton imgshouye;
    private ImageButton imgshujia;
    private ImageButton imggeren;






    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();//初始化控件
        initEvents();//为四个LinearLayout添加点击事件
        setSelect(0);  //让第一个fragment进行显示

        Intent intent = getIntent();
        int id = intent.getIntExtra("flag",2);
        if(id>0){
            mviewpager.setCurrentItem(2);
        }
    }

    private void initEvents() {  //为三个按钮设置点击事件
        mshouye.setOnClickListener(this);
        mshujia.setOnClickListener(this);
        mgeren.setOnClickListener(this);

    }

    private void initView() {  //初始化
        mviewpager = (ViewPager) findViewById(R.id.id_content);//关联viewpager

        mshouye = (LinearLayout) findViewById(R.id.shouye);//初始化LinearLayout
        mshujia = (LinearLayout) findViewById(R.id.shujia);
        mgeren = (LinearLayout) findViewById(R.id.geren);

        imgshouye = findViewById(R.id.b1);//初始化图标按钮
        imgshujia = findViewById(R.id.b2);
        imggeren = findViewById(R.id.b3);


        mFragments = new ArrayList<Fragment>();  //初始化数据源
        Fragment shouye1 = new shouyeFragment();
        Fragment shujia1 = new shujiaFragment();
        Fragment geren1 = new gerenFragment();

        mFragments.add(shouye1);
        mFragments.add(shujia1);
        mFragments.add(geren1);

        mAdpter = new FragmentPagerAdapter(getSupportFragmentManager()) { //设置适配器
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }//返回不同的fragment
            @Override
            public int getCount() {
                return mFragments.size();
            }//返回数据源的数量
        };
        mviewpager.setAdapter(mAdpter);
        mviewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                int currentItem = mviewpager.getCurrentItem();
                setTab(currentItem);
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private void setSelect(int i) {
        setTab(i);
        mviewpager.setCurrentItem(i);
    }

    private void setTab(int i) {  //切换图片
        resetImgs();
        switch (i) {
            case 0:
                imgshouye.setImageResource(R.drawable.syl);
                break;
            case 1:
                imgshujia.setImageResource(R.drawable.sjl);
                break;
            case 2:
                imggeren.setImageResource(R.drawable.grl);
                break;
            default:
                break;
        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.shouye:
                setSelect(0);
                break;
            case R.id.shujia:
                setSelect(1);
                break;
            case R.id.geren:
                setSelect(2);
                break;
        }

    }
        //切换图片至暗色

        private void resetImgs(){
            imgshouye.setImageResource(R.drawable.sy);
            imgshujia.setImageResource(R.drawable.sj);
            imggeren.setImageResource(R.drawable.gr);

    }
}






